-- ======================================================================
-- Hell Lua Bus
-- 
-- Copyright 2012-2013Hell-Prototypes
--
-- http://www.hellprototypes.com/
--
-- This is free software, licensed under the terms of the GNU General
-- Public License as published by the Free Software Foundation.
-- ======================================================================
hlb = require('libhlb')

function device_open()
	local ret = hlb.usb_open(0x04B4FFFF)
	if ret < 0 then
		print("Fail to open usb device")
		return false
	end
	
	return true
end

function device_close()
	local ret = hlb.usb_close()
	if ret < 0 then
		print("Fail to close usb device")
		return false
	end
	
	return true
end

function pause()
    io.write("\r\nAny key to continue ...")
    io.flush()
    io.read()
    print("")
end
-- -----------------------------------------------
function ctrl_5V(cmd_data)
    if type(cmd_data) ~= "number" then
        print("cmd_data must be number type")
        return false
    end

    local msg = {0x40, 0xC5, cmd_data, 0,0, 100}
    local ret

    ret = hlb.usb_control_msg(msg)
    if ret < 0 then
        print("ctrl_5V() ret = " .. ret)
        return false
    end

    return true
end
-- -----------------------------------------------
ret = device_open()
if ret == false then
    print("device_open fail")
    return
end

ret = ctrl_5V(100)
if ret == false then
    print("ctrl_5V() exec fail 0")
    return
end

pause()

ret = ctrl_5V(0)
if ret == false then
    print("ctrl_5V() exec fail 0")
    return
end

device_close()
